package com.chencs.utils;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chencs.biz.Common;
import com.chencs.vo.biz.password.ApplyCommission;
import com.chencs.vo.biz.password.Commission;
import org.jsoup.Jsoup;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/11/27.
 */
public class ApplyCommissionUtils {

    /**
     * 商品ID 招高佣金
     * @return
     */
    public static Commission getCommissionForPid(String pid) {
        Commission commission  = new Commission();
        Map<String,String>  map = getSiyouquan(pid);
        if(map==null){
            return null;
        }
        commission.setCommission_url(map.get("click"));
        commission.setCommission_coupon(map.get("url"));
        commission.setPrice(map.get("price"));
        commission.setCoupon_info(map.get("coupon_info"));
        return  commission;
    }







    /**
     * 申请高佣金
     * @param fid
     * @param mid
     * @param url
     * @return
     */
    public static ApplyCommission get(String fid,String mid,String url) {
        ApplyCommission commission  = new ApplyCommission();
        String str = "-1";
        String mmid = mid;
        String flag ="0";
        if(com.alibaba.druid.util.StringUtils.isEmpty(url)){
            return null;
        }
        String pid = ApplyCommissionUtils.getUrlMap(CRequest.URLRequest(url));
       // System.out.println("pid:"+pid);
            if(com.alibaba.druid.util.StringUtils.isEmpty(mmid)){
                mmid = Common.PID;
                flag = "-1";
            }
            try {
                if(!StringUtils.isEmpty(pid)){
                    if(StringUtils.isNumber(pid)){
                        org.jsoup.nodes.Document res = Jsoup.connect("http://tkapi.apptimes.cn/coupon?appkey=93kc2a4b&pid="+mmid+"&good_id="+pid)
                                .timeout(10000).ignoreContentType(true).get();
                        str = res.body().text();
                        commission = JSON.parseObject(str,ApplyCommission.class);
                    }else{
                        commission.setErrcode("-1");
                        commission.setErrmsg("pid不是数字");
                    }
                }else{
                    commission.setErrcode("-1");
                    commission.setErrmsg("pid空");
                }
                commission.setFid(fid);
                commission.setFlag(flag);
                commission.setMid(mid);
                commission.setUrl(url);
                commission.setPid(pid);

            } catch (Exception e) {
                commission.setFid(fid);
                commission.setFlag(flag);
                commission.setMid(mid);
                commission.setUrl(url);
                commission.setPid(pid);
                commission.setErrmsg("异常");
                return commission;
            }

        return commission;
    }


    public static String getUrlMap(Map<String,String> map){

        String pid = "";

        if(map.get("item_id")!=null){
            pid = map.get("item_id");
        }else{
            pid = map.get("id");
        }

        return  pid;
    }

    //检查券是否有效
    public  static Map<String,String>  checkCounp(String urls){
        Map<String,String> map = new HashMap<>();

       System.out.println("E="+urls);
        String str = "";
        try {
            org.jsoup.nodes.Document res = Jsoup.connect("https://uland.taobao.com/cp/coupon?e="+urls)
                    .timeout(10000).ignoreContentType(true).get();
            str = res.body().text();
         //   System.out.println(str);
            JSONObject obj = JSON.parseObject(str);
            if(true){ //有效
                map.put("state",str);
                map.put("clcik",obj.getString("commission_coupon"));

                map.put("flag","0");
                map.put("e",urls);
                return  map;
            }else{
                map.put("flag","-1");
                map.put("e",urls);
                map.put("clcik",obj.getJSONObject("item").getString("clickUrl"));
                return  map;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    //拿到高佣金连接地址
    public static void getUrl(String pid){

    }






    //拿私有券
    public  static  Map<String,String>  getSiyouquan(String pid){
        Map<String,String> map = new HashMap<>();
        try { //http://tkapi.apptimes.cn/coupon?appkey=93kc2a4b&pid=mm_16510629_18916799_68766966&good_id="+pid
            org.jsoup.nodes.Document res = Jsoup.connect("http://tkapi.apptimes.cn/coupon?appkey=93kc2a4b&pid="+Common.PID+"&good_id="+pid)
                    .timeout(10000).ignoreContentType(true).get();
            String str = res.body().text();
            System.out.println(str);
            ApplyCommission  commission = JSON.parseObject(str,ApplyCommission.class);
            if(commission.getErrcode().equals("0")){
                Map<String,String> map1 = new HashMap<>();
                if(true){//有效的券
                    map.put("click",commission.getData().getCoupon_click_url());
                    map.put("url",commission.getData().getCoupon_click_url());
                    map.put("coupon_info",commission.getData().getCoupon_info());
                    map.put("price",commission.getData().getMax_commission_rate());
                    map.put("flag","1");
                    return  map;
                }else{ //不管怎么样子也返回高佣金连接

                    map.put("flag",map1.get("flag"));
                    System.out.println("无效");
                    map.put("click",map1.get("clcik"));
                    return  map;
                }
            }

        }catch (Exception e){
            e.printStackTrace();
            return null;
        }

        return  map;
    }



//    //拿私有券
//    public  static  Map<String,String>  getSiyouquan(String pid){
//        Map<String,String> map = new HashMap<>();
//
//        try { //http://tkapi.apptimes.cn/coupon?appkey=93kc2a4b&pid=mm_16510629_18916799_68766966&good_id="+pid
//            org.jsoup.nodes.Document res = Jsoup.connect("http://tkapi.apptimes.cn/coupon?appkey=93kc2a4b&pid="+Common.PID+"&good_id="+pid)
//                    .timeout(10000).ignoreContentType(true).get();
//            String str = res.body().text();
//           System.out.println(str);
//            ApplyCommission  commission = JSON.parseObject(str,ApplyCommission.class);
//            if(commission.getErrcode().equals("0")){
//             //  System.out.println("可以用");
//                String e  = commission.getData().getCoupon_click_url().split("e=")[1];
//                Map<String,String> map1 = checkCounp(e);
//               if(map1!=null){//有效的券
//                   System.out.println("有效");
//                   map.put("e",e);
//                   map.put("click",map1.get("clcik"));
//                   map.put("url",commission.getData().getCoupon_click_url());
//                   map.put("coupon_info",commission.getData().getCoupon_info());
//                   map.put("price",map1.get("price"));
//                   map.put("flag",map1.get("flag"));
//                   return  map;
//               }else{ //不管怎么样子也返回高佣金连接
//                   map.put("e",e);
//                   map.put("flag",map1.get("flag"));
//                   System.out.println("无效");
//                   map.put("click",map1.get("clcik"));
//                   return  map;
//               }
//            }
//
//        }catch (Exception e){
//            e.printStackTrace();
//            return null;
//        }
//
//        return  map;
//    }


//
//    public static void main(String[] args) {
//        //    String str = ApplyCommissionUtils.get("561807293177");
////////        String ju="https://detail.ju.taobao.com/home.htm?spm=608.5847457.318.4.455033dfcosL8K&id=10000063752110&item_id=552981443751";
//////  //     String tm ="https://detail.tmall.com/item.htm?id=18565653237&spm=a219t.7900221/17.1998910419.d9a1dac8e20k.5cd4b9ec2reBJ1&sku_properties=122216909:3817801";
//////        String tb="https://detail.tmall.com/item.htm?id=548061166153";
//////
//      System.out.println(ApplyCommissionUtils.getSiyouquan("557766363358").toString());
//////
//////
////////        ApplyCommission applyCommissionUtils  = ApplyCommissionUtils.get(null,null,tb);
////////
////////        String coupon_click_url = "https://uland.taobao.com/coupon/edetail?e=zBFCTGMw5OoGQASttHIRqQvvwpGZUdWdxjViUDvsF4icCWxV4QYCqFO7729C6Yojf6DCjihW%2FRFbOF%2BQang2FZBh%2BsFgnewCt5bBKNQlPfvYhpVVy38fp9pR0UZ0ttC1&traceId=0bb9f35915119624307076479e";
////////
////////        System.out.println( JSON.toJSON(ApplyCommissionUtils.checkCounp(applyCommissionUtils.getData().getCoupon_click_url())));
////
////
//   }
}
